<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar" id="tbToolBar">
                <div class="layui-form-item">
					<div class="layui-inline">
						<button id="btnAddBizScene" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>创建场景</button>
					</div>
                    <div class="layui-inline" style="float: right;margin-right: 0px;">
						<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
							<label class="layui-form-label w-auto">场景</label>
							<div class="layui-input-inline mr0">
								<input name="name" class="layui-input" type="text" placeholder="请输入场景名称"/>
							</div>
						</div>
						<div class="layui-inline"
							style="padding-right: 0px; margin-right: 0px;">
							<select name="isPublic"
								style="width: 110px; height: 38px; border-style: solid; border-radius: 2px; border-color: #e6e6e6;">
								<option value="">--请选择--</option>
								<option value="0">公开</option>
								<option value="1">私有</option>
							</select>
						</div>						
						<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
							<button class="layui-btn icon-btn" lay-filter="formSubSearchBizScene" lay-submit>
								<i class="layui-icon">&#xe615;</i>查询
							</button>
						</div>					
					</div>
				</div>
				<table class="layui-table" id="tableBizScene" lay-filter="tableBizScene"></table>
			</div>
		</div>
	</div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarBizScene">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>
 	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="menu">配置</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="v">预览</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="tableIsPublicBizScene">
#[[  {{#  if(d.isPublic === '0'){ }}
    <span style="color: #23aa2e;">公开</span>
  {{#  } else { }}
	<span style="color: #ff0000;">私有</span>
  {{#  } }}
]]#
</script>


<script type="text/html" id="modelBizSceneMenuScript">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearchAuth" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearchBizSceneMenu" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAddBizSceneMenu" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>新增</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableBizSceneMenu" lay-filter="tableBizSceneMenu"></table>
        </div>
    </div>
</script>


<!-- 表单弹窗 -->
<script type="text/html" id="modelBizSceneMenuFormScript"> 
    <form id="modelMenuForm" lay-filter="modelMenuForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
		<input name="sceneId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">上级</label>
            <div class="layui-input-block">
				<input id="menuParentId" type="text" name="parentId"   lay-filter="parentId" autocomplete="off" placeholder="请选择" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标识</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入标识" type="text" class="layui-input" maxlength="50" />
            </div>
        </div>        
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
		      <select name="type" lay-filter="type" lay-verType="tips" lay-verify="required" required>
		        <option value=""></option>
		        <option value="0">目录</option>
				<option value="MENU_GROUP">菜单组</option>
		        <option value="MENU">菜单</option>
				<option value="BTN_GROUP">按钮组</option>
		        <option value="BTN">按钮</option>
		        <option value="LAYER_GROUP">图层组</option>
				<option value="ASSEMBLY_GROUP">组件组</option>
				<option value="ASSEMBLY">组件</option>
				<option value="LAYER">图层</option>
				<option value="BASE_LAYER_GROUP">默认图层</option>
		      </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">接口地址</label>
            <div class="layui-input-block">
                <input name="url" placeholder="请输入接口地址" type="text" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">接口类型</label>
            <div class="layui-input-block">
		      <select name="apiType" lay-filter="apiType" >
		        <option value=""></option>
		        <option value="HTTP">HTTP</option>
		        <option value="SOCKET">SOCKET</option>
		      </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">接口来源</label>
            <div class="layui-input-block">
		      <select name="apiSrc" lay-filter="apiSrc" >
		        <option value=""></option>
		        <option value="0">内部接口</option>
		        <option value="1">第三方接口</option>
		      </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图标</label>
            <div class="layui-input-block">
                <input name="icon" placeholder="请输入图标" type="text" class="layui-input"/>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">图层</label>
            <div class="layui-input-block">
				<input id="layerId" type="text" name="layerId"   lay-filter="layerId" autocomplete="off" placeholder="请选择" value="" class="layui-input">
            </div>
        </div> 
        <div class="layui-form-item">
            <label class="layui-form-label">图层组</label>
            <div class="layui-input-block">
		      <select name="layerGroup" lay-filter="layerGroup">
		        <option value=""></option>
		        #for(group :groups)
					<option value="#(group.code)">#(group.name)</option>
				#end
		      </select>            
			</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">默认显示</label>
            <div class="layui-input-block">
		      <select name="layerShow" lay-filter="layerShow">
		        <option value=""></option>
		        <option value="0">否</option>
		        <option value="1">是</option>
		      </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图层可控</label>
            <div class="layui-input-block">
		      <select name="layerSwitch" lay-filter="layerSwitch">
		        <option value=""></option>
		        <option value="0">否</option>
		        <option value="1">是</option>
		      </select>
            </div>
        </div>                

        <div class="layui-form-item">
            <label class="layui-form-label">显示层级</label>
            <div class="layui-input-block">
                <input name="layerLevel" placeholder="请输入层级" type="number" class="layui-input" min="0" max="23" lay-verType="tips" lay-verify="number" />
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入序号" type="number" class="layui-input" min="0" max="1000" lay-verType="tips" lay-verify="number" />
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitMenu" lay-submit>保存</button>
        </div>
    </form>
</script>

<!--  

	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">按钮组</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">图层组</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">组件组</a>

	 -->
<!-- 表格操作列 --> 
<script type="text/html" id="tableBarBizSceneMenuScript">
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 菜单类型 --> 
<script type="text/html" id="bizSceneMenuTypeTpl">
#[[
  {{#  if(d.type === '0' ){ }}
	<img src="./assets/img/目录文件夹.png" />
  {{#  } else if(d.type === 'MENU_GROUP' ) { }}
	<img src="./assets/img/菜单.png" />
  {{#  } else if(d.type === 'MENU' ) { }}
	<span class="layui-badge-rim" >菜单</span>
  {{#  } else if(d.type === 'BTN_GROUP' ) { }}
	<img src="./assets/img/按钮.png" />
  {{#  } else if(d.type === 'BTN' ) { }}
	<span class="layui-badge-rim" >按钮</span>
  {{#  } else if(d.type === 'LAYER_GROUP' ) { }}
	<img src="./assets/img/图层.png" />
  {{#  } else if(d.type === 'LAYER' ) { }}
	<span class="layui-badge-rim">图层</span>
  {{#  } else if(d.type === 'BASE_LAYER_GROUP' ) { }}
	<img src="./assets/img/地图.png" />
  {{#  } else if(d.type === 'ASSEMBLY_GROUP' ) { }}
	<span class="layui-badge-rim" >组件组</span>
  {{#  } else if(d.type === 'ASSEMBLY' ) { }}
	<span class="layui-badge-rim" >组件</span>
  {{#  } else { }}
	<span class="layui-badge-rim" >未知</span>
  {{#  } }}
]]#
</script>

<!-- js部分 -->
<script>
layui.use(['layer', 'form', 'table', 'util', 'admin', 'treetable','config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var treetable = layui.treetable;
        form.render('select');
        // 渲染表格
        var insTb = table.render({
            elem: '#tableBizScene',
            url: '#(ctx)/biz/scene/listData', 
            page: true,
            toolbar: false,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'id', sort: false, title: '场景id',width: 270},
                {field: 'title', sort: false, title: '场景名称'},
				{field: 'isPublic', sort: false, templet: '#tableIsPublicBizScene', title: '状态', align: 'center'},
				{field: 'createUserName', sort: false, title: '来源于'},
                {
                    field: 'createTime', sort: false, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
				{align: 'left', toolbar: '#tableBarBizScene', title: '操作', fixed: 'right',width: 250}
            ]],
        });

        // 添加
		$('#btnAddBizScene').click(function () {
	        layer.load(2);
	        var formData={}; 
	        formData['bizScene.title']='未命名';
              	admin.req("biz/scene/save",formData, function (res) {
                  layer.closeAll('loading');
                  if (res.code == 200) {
                      layer.msg(res.message, {icon: 1});
                      insTb.reload({}, 'data');
                  } else {
                      layer.msg(res.message, {icon: 2});
                  }
            }, 'get');
			 
		});
        // 搜索
        form.on('submit(formSubSearchBizScene)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableBizScene)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
            	 window.open('#(ctx)/scene/editor?sceneId='+data.id,"_blank");
            } else if (layEvent === 'del') { 
                doDel(data.id, data.title);
            }
            else if('menu'==layEvent){
            	showMenuListModel(data);
            }
            else if('v'==layEvent){
           	 	window.open('#(ctx)/v?sceneId='+data.id,"_blank");
            }
        });

        // 删除
        function doDel(id, name) {
        	layer.confirm('确定要删除“【<span style="color:red;">' + name + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('biz/scene/delete', {
                    id: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }
        

        // 显示表单弹窗
        function showMenuListModel(data) {
            admin.open({
                type: 1,
                title:'【'+data.title+'】配置',
                content: $('#modelBizSceneMenuScript').html(),
                area: ['800px', '600px'],
                offset: 't'	,
                fixed :true,
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    renderSceneMenuTreeTable(data.id);
                    
                    //添加
                    $('#btnAddBizSceneMenu').click(function () {
                        showEditSceneMenuModel(data.id,null);
                    });
                    
                    table.on('tool(tableBizSceneMenu)', function (obj) {
                        var menu = obj.data;
                        var layEvent = obj.event;
                        if (layEvent === 'edit') { // 修改
                        	showEditSceneMenuModel(data.id,menu);
                        } else if (layEvent === 'del') { // 删除
                            doDelSceneMenu(data.id,menu);
                        }
                    });
                }
            });
        }
        
        
        function renderSceneMenuTreeTable(id) {
            treetable.render({
                treeColIndex: 1,
                treeSpid: id,
                treeIdName: 'id',
                treePidName: 'parentId',
                elem: '#tableBizSceneMenu',
                url:'#(ctx)/biz/scene/menu/listData',
                cellMinWidth: 100,
                height: 800,
                where:{
                	sceneId:id
                },
                cols: [[
                    {type: 'numbers'},
                    {field: 'name', title: '名称', minWidth: 200, align: 'left'},
                    {title: '类型', templet: '#bizSceneMenuTypeTpl', align: 'left'},
                    { toolbar: '#tableBarBizSceneMenuScript', title: '操作', align: 'left', minWidth: 150}
                ]]
            });
        }
        
        function showEditSceneMenuModel(sceneId,menu) {
            admin.open({
                type: 1,
                title: (menu ? '修改' : '添加') + '场景菜单',
                content: $('#modelBizSceneMenuFormScript').html(),
                area:'400px',
                offset: 't'	,
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = 'biz/scene/menu/save';
                    form.val('modelMenuForm', menu);  // 回显数据
            	    
                    treeSelect.render({
            	        elem: '#menuParentId',
            	        data: '#(ctx)/scene/menu/getTree?sceneId='+sceneId,
            	        type: 'get',
            	        placeholder: '请选择',
            	        search: true,
            	        click: function(d){
            	        },
            	        success: function (d) {
            	        	if(menu && menu.parentId!=sceneId)
                        		treeSelect.checkNode('parentId', menu.parentId);
            	        }
            	    });
                    
                    treeSelect.render({
            	        elem: '#layerId',
            	        data: '#(ctx)/scene/layer/getTree?sceneId='+sceneId,
            	        type: 'get',
            	        placeholder: '请选择',
            	        search: true,
            	        click: function(d){
            	        },
            	        success: function (d) {
            	        	if(menu && menu.layerId)
                        		treeSelect.checkNode('layerId', menu.layerId);
            	        }
            	    });
                    
                    // 表单提交事件
                    form.on('submit(modelSubmitMenu)', function (data) {
                        if (data.field.parentId == '') {
                            data.field.parentId = sceneId;
                        }
                        if (data.field.sceneId == '') {
                            data.field.sceneId = sceneId;
                        }
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['bizSceneMenu.'+key]=data.field[key];
                        }
                        
                        admin.req(url,formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                renderSceneMenuTreeTable(sceneId)
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }
        
        function doDelSceneMenu(sceneId,data) {
        	layer.confirm('确定要删除“【<span style="color:red;">' + data.name + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('biz/scene/menu/delete', {
                    id: data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        renderSceneMenuTreeTable(sceneId);
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }
        

    });
</script>
